package cn.cj.erp.modules.monitor.controller;

import cn.cj.erp.common.redis.RedisKeyEnum;
import cn.cj.erp.common.vo.monitor.server.Server;
import cn.hutool.core.util.ObjectUtil;
import cn.cj.erp.common.constants.SystemConstants;
import cn.cj.erp.common.http.Result;
import cn.cj.erp.common.redis.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 服务器监控
 *
 * @author chenjian
 */
@Slf4j
@RestController
@RequestMapping("/monitor/server")
public class ServerController {
    @Resource
    private RedisCache redisCache;

    @PreAuthorize("@ss.hasPerm('monitor:server:list')")
    @GetMapping("/info")
    public Result<Server> getInfo() {
        Server server = redisCache.get(RedisKeyEnum.SERVER_INFO.getKey(""), Server.class);
        if (ObjectUtil.isNotNull(server)) {
            log.info("缓存找到服务器信息");
            return Result.ok(server);
        }
        log.info("缓存未找到服务器信息，重新获取");
        server = new Server();
        server.copyTo();
        redisCache.set(RedisKeyEnum.SERVER_INFO.getKey(""), server, SystemConstants.EXPIRE_REDIS_SERVER_INFO);
        return Result.ok(server);
    }
}
